//
//  LWLoginViewController.m
//  IOSResume
//
//  Created by wynn on 16/4/20.
//  Copyright © 2016年 wynn. All rights reserved.
//

#import "LWLoginViewController.h"
#import "LWRegisterViewController.h"
#import "RQShineLabel.h"

@interface LWLoginViewController ()

@property (nonatomic, strong) UITextField *usernameTextField;
@property (nonatomic, strong) UITextField *passwordTextField;
@property (nonatomic, strong) UIButton *loginButton;
@property (nonatomic, strong) UIButton *registerButton;
@property (nonatomic, strong) UIButton *backButton;
@property (nonatomic, strong) UIImageView *backGroundImageView;
@property (nonatomic, strong) RQShineLabel *shineLabel;

@end

@implementation LWLoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    if (!_backGroundImageView) {
        // 设置背景图片
        UIImage *backGroundImage = [UIImage imageNamed:@"defaultBackground"];
        self.backGroundImageView = [[UIImageView alloc] initWithImage:backGroundImage];
        self.backGroundImageView.frame = self.view.bounds;
        [self.view addSubview:self.backGroundImageView];
        // 用户名输入框
        self.usernameTextField = [[UITextField alloc] init];
        self.usernameTextField.frame = CGRectMake(50.0, startMargin + 64.0, SCREEN.width - 100.0, 40.0);
        self.usernameTextField.textColor = [UIColor whiteColor];
        self.usernameTextField.placeholder = @"邮箱";
        [self.usernameTextField setValue:[UIColor whiteColor] forKeyPath:@"_placeholderLabel.textColor"];
        [self.view addSubview:self.usernameTextField];
        self.usernameTextField.layer.borderWidth = 1.0;
        self.usernameTextField.layer.borderColor = [UIColor whiteColor].CGColor;
        self.usernameTextField.layer.cornerRadius = 20.0;
        self.usernameTextField.textAlignment = NSTextAlignmentCenter;
        // 密码输入框
        self.passwordTextField = [[UITextField alloc] init];
        self.passwordTextField.frame = CGRectMake(50.0, startMargin + 64.0 + 40.0 + 20.0, SCREEN.width - 100.0 , 40.0);
        self.passwordTextField.textColor = [UIColor whiteColor];
        self.passwordTextField.placeholder = @"密码";
        [self.passwordTextField setValue:[UIColor whiteColor] forKeyPath:@"_placeholderLabel.textColor"];
        [self.view addSubview:self.passwordTextField];
        self.passwordTextField.textAlignment = NSTextAlignmentCenter;
        self.passwordTextField.secureTextEntry = YES;
        self.passwordTextField.layer.borderWidth = 1.0;
        self.passwordTextField.layer.borderColor = [UIColor whiteColor].CGColor;
        self.passwordTextField.layer.cornerRadius = 20.0;
        self.passwordTextField.textColor = [UIColor whiteColor];
        self.passwordTextField.borderStyle = UITextBorderStyleNone;
        // 登陆按钮
        self.loginButton = [UIButton buttonWithType:UIButtonTypeCustom];
        self.loginButton.frame = CGRectMake(50.0, startMargin + 64.0 + 40.0 + 20.0 + 40.0 + 20.0, SCREEN.width - 100.0 , 40.0);
        [self.loginButton setTitle:@"登陆" forState:UIControlStateNormal];
        [self.view addSubview:self.loginButton];
        [self.loginButton setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
        self.loginButton.titleLabel.textAlignment = NSTextAlignmentCenter;
        self.loginButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
        [self.loginButton setBackgroundColor:APP_COLOR/*COLOR(0, 174, 106)*/];
        self.loginButton.layer.cornerRadius = 20.0;
        [self.loginButton addTarget:self action:@selector(actionLogin:) forControlEvents:UIControlEventTouchUpInside];
        [self.loginButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        self.loginButton.clipsToBounds = YES;
        // 注册按钮
        self.registerButton = [[UIButton alloc] init];
        self.registerButton.frame = CGRectMake(SCREEN.width - 50.0 - 100.0, startMargin + 64.0 + 40.0 + 20.0 + 40.0 + 20.0 + 40.0 + 20.0, 100.0 , 40.0);
        [self.registerButton setTitle:@"注册" forState:UIControlStateNormal];
        [self.view addSubview:self.registerButton];
        [self.registerButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [self.registerButton setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
        self.registerButton.titleLabel.textAlignment = NSTextAlignmentRight;
        self.registerButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
        [self.registerButton addTarget:self action:@selector(actionRigster:) forControlEvents:UIControlEventTouchUpInside];
        self.registerButton.layer.cornerRadius = 5.0;
        self.registerButton.clipsToBounds = YES;
        // 轻度提示词
        self.shineLabel = [[RQShineLabel alloc] init];
        self.shineLabel.frame = CGRectMake(50.0, startMargin + 64.0 + 40.0 + 20.0 + 40.0 + 20.0  + 40.0 + 20.0 + 40.0 + 20.0, SCREEN.width - 100.0 , 40.0);
        self.shineLabel.textAlignment = NSTextAlignmentCenter;
        [self.view addSubview:self.shineLabel];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - IBAction

- (void)actionLogin:(UIButton *)button
{
    if (![LWEvaluateUtil validateEmail:self.usernameTextField.text]) {
        self.shineLabel.text = @"请输入合法的邮箱";
        [self.shineLabel shine];
        return;
    }
    if (![LWEvaluateUtil validatePassword:self.passwordTextField.text]) {
        self.shineLabel.text = @"密码不小于6位";
        [self.shineLabel shine];
        return;
    }
    [BmobUser loginInbackgroundWithAccount:self.usernameTextField.text andPassword:self.passwordTextField.text block:^(BmobUser *user, NSError *error) {
        if (user)
        {
            [self.navigationController popViewControllerAnimated:YES];
        }
        else
        {
            if(error.code==101){
                [SVProgressHUD showErrorWithStatus:@"用户名或密码错误,请重新输入"];
            }
        }
    }];
}

- (void)actionRigster:(UIButton *)button
{
    LWRegisterViewController *vc = [[LWRegisterViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}

@end
